#include <GameEngine/Graphics/Meshes/trgl_mesh_group.h>


#ifdef GAMEENGINE_NAMESPACES
namespace GameEngine {
#endif

//=================================================================================

//! Default constructor
TrglMeshGroup::TrglMeshGroup() : name_("Default") {}

//! Copy constructor
/*!
 @param rhs The triangle group to copy
 */
TrglMeshGroup::TrglMeshGroup(const TrglMeshGroup& rhs) : name_(rhs.name_), subgroups_(rhs.subgroups_) {}

//! Destructor
TrglMeshGroup::~TrglMeshGroup() {}

//! Assignment operator
/*!
 @param rhs The triangle group to copy
 */
TrglMeshGroup& TrglMeshGroup::operator=(const TrglMeshGroup& rhs) {
	if ( &rhs != this ) {
		name_ = rhs.name_;
		subgroups_ = rhs.subgroups_;
	}
	return *this;
}

//! Accessor to the name of the triangle group (read only)
/*!
 @return A constant reference to the name of the triangle group
 */
const GEString& TrglMeshGroup::name() const {
	return name_;
}

//! Accessor to the name of the triangle group (read-write)
/*!
 @return A reference to the name of the triangle group
 */
GEString& TrglMeshGroup::name() {
	return name_;
}

//! Accessor to the subgroups of the triangle group (read only)
/*!
 @return A constant reference to the subgroups of the triangle group
 */
const std::vector<TrglMeshSubGroup>& TrglMeshGroup::subgroups() const {
	return subgroups_;
}

//! Accessor to the subgroups of the triangle group (read-write)
/*!
 @return A reference to the subgroups of the triangle group
 */
std::vector<TrglMeshSubGroup>& TrglMeshGroup::subgroups() {
	return subgroups_;
}

//! Accessor to the number of faces in the triangle group
/*!
 @return The number of faces in the triangle group
 */
SurfaceMesh::FaceCount TrglMeshGroup::face_count() const {
	SurfaceMesh::FaceCount res = 0;

	// Iterate over the triangle subgroups
	for ( std::vector<TrglMeshSubGroup>::const_iterator it = subgroups_.begin(); it != subgroups_.end(); ++it ) {
		res += it->face_count();
	}

	// Return result
	return res;
}

//! Clear the triangle group
/*!
 *  This function removes all the subgroups from the triangle group
 *  and resets its name to "Default".
 */
void TrglMeshGroup::clear() {
	subgroups_.clear();
	name_ = "Default";
}

//! Render all the triangle subgroups that match the transparency flag
/*!
 @param ray The picking ray under the mouse
 @param mesh The mesh to which the subgroup belongs
 @param transparency Determines whether transparent or opaque subgroups should be rendered
 @return True if some triangles were rendered, false otherwise
 */
bool TrglMeshGroup::render_group(const Line3d<float>& ray, const TrglMesh& mesh, bool transparency) {
	// Iterate over the triangle subgroups
	bool res = false;
	for ( std::vector<TrglMeshSubGroup>::iterator it = subgroups_.begin(); it != subgroups_.end(); ++it ) {
		if ( it->has_transparency() != transparency ) continue;
		it->render_subgroup(ray,mesh);
		res = true;
	}

	return res;
}

//=================================================================================

#ifdef GAMEENGINE_NAMESPACES
} // namespace GameEngine
#endif
